using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.BLL;
using Pheonix.CodeLibrary.Web;

public partial class control_LoginBox : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((TextBox)loginBox.FindControl("userName")).Attributes.Add("onclick", "javascript:this.value='';javascript:this.style.color='#cc0000';");
        ((RegularExpressionValidator)loginBox.FindControl("emailValidate")).ValidationExpression = RegExUtil.EmailFormat;
        ((RegularExpressionValidator)loginBox.FindControl("passwordValidate")).ValidationExpression = RegExUtil.PasswordFormat;
        ((ImageButton)loginBox.FindControl("logon")).ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/login.jpg";
    }

    protected void validateLogon_Validate(object sender, ServerValidateEventArgs e)
    {
        // Filter the user inputs for unwanted charecters
        string userName = Utility.FilterInputText(((TextBox)loginBox.FindControl("userName")).Text);
        string password = Utility.FilterInputText(((TextBox)loginBox.FindControl("password")).Text);

        // Perform login and save CurrentUser data in session
        SessionManager.LoggedUser = UserManager.LogonUser(userName,password);

        // If no valid login data was found
        if (!SessionManager.IsUserLoggedIn)
        {
            // validation fails
            e.IsValid = false;
            // Setting error message
            ((CustomValidator)loginBox.FindControl("validateLogon")).ErrorMessage = "Login Failed";
        }
        else
        {
            // otherwise validation succeeds
            e.IsValid = true;
        }
    }

    protected void logon_Click(object sender, EventArgs e)
    {
        // If validation succeeds, redirect to homepage
        if(Page.IsValid)
            Response.Redirect("~/private/MyAccount.aspx");
    }
}
